package com.hy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hy.common.Result;
import com.hy.entity.Weekly;
import com.hy.service.SysWeeklyService;
import com.hy.service.WeeklyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/admin/weekly")
public class SysWeeklyController {

    @Autowired
    private SysWeeklyService weeklyService;


    @Value("${upload.weekly-pdf-path}")  // 配置文件中的PDF存储路径
    private String uploadPath;

    /**
     * 上传周刊PDF文件，仅返回可访问路径
     */
    @PostMapping("/uploadPdf")
    public Result<String> uploadWeeklyPdf(@RequestParam("file") MultipartFile file) {
        try {
            // 校验文件
            if (file.isEmpty()) {
                return Result.error("请选择PDF文件");
            }
            if (!file.getContentType().equalsIgnoreCase("application/pdf")) {
                return Result.error("请上传PDF格式的文件");
            }

            // 生成唯一文件名
            String originalFilename = file.getOriginalFilename();
            String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName = UUID.randomUUID() + fileSuffix;

            // 创建存储目录
            File destDir = new File(uploadPath);
            if (!destDir.exists()) {
                destDir.mkdirs();
            }

            // 保存文件到本地
            File destFile = new File(uploadPath + newFileName);
            file.transferTo(destFile);

            // 返回可访问的URL
            String accessUrl = "/weekly-pdf/" + newFileName;
            return Result.success(accessUrl);
        } catch (Exception e) {
            return Result.error("上传失败：" + e.getMessage());
        }
    }


    // 创建周刊
    @PostMapping("/createWeekly")
    public Result<Weekly> createWeekly(@RequestBody Weekly weekly) {
        try {
            return weeklyService.createWeekly(weekly);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("创建周刊失败");
    }

    // 获取所有周刊
    // 分页获取周刊信息
    @GetMapping("/getWeeklyPage")
    public Result<IPage<Weekly>> getWeeklyPage(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Integer minLikes,
            @RequestParam(required = false) Integer maxLikes
            ) {
        try {
            return weeklyService.getWeeklyPage(pageNum, pageSize, title, minLikes, maxLikes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("分页获取周刊信息失败");
    }

    // 根据ID获取周刊
    @GetMapping("/getWeeklyById")
    public Result<Weekly> getWeeklyById(@RequestParam("id") Long id) {
        try {
            return weeklyService.getWeeklyById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("未找到该周刊");
    }

    // 更新周刊
    @PostMapping("/updateWeekly")
    public Result<Weekly> updateWeekly(@RequestBody Weekly weekly) {
        try {
            return weeklyService.updateWeekly(weekly);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("更新周刊失败");
    }

    // 根据ID删除周刊
    @DeleteMapping("/deleteWeekly")
    public Result<String> deleteWeekly(@RequestParam("id") Long id) {
        try {
            return weeklyService.deleteWeekly(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("删除周刊失败");
    }
}